
package net.lab0.fractal.main;


import java.awt.Dimension;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import net.lab0.fractal.concurrent.MainThreadPoolExecutor;
import net.lab0.fractal.conf.ApplicationConfiguration;
import net.lab0.fractal.exception.IllegalOperationException;
import net.lab0.fractal.gui.MainWindow2;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;


public class Main
{
    private static Logger logger = Logger.getLogger(Main.class);
    
    public static void main(String[] args)
    {
        logger.info("--START--");
        logger.info("Application started at " + new Date() + "(timestamp:" + System.currentTimeMillis() + ")");
        logger.info("Logging system initialized");
        
        try
        {
            ApplicationConfiguration.init();
        }
        catch (Exception e)
        {
            logger.fatal("Failed to initiate application configuration", e);
        }
        
        Runtime.getRuntime().addShutdownHook(new Thread("Logging shutdown thread")
        {
            @Override
            public void run()
            {
                logger.info("Shutting down logging system");
                logger.info("--STOP--");
                LogManager.shutdown();
            }
        });
        
        System.out.println(ApplicationConfiguration.getConfigurationRoot());
        
        logger.info("Initiating main thread pool executor");
        try
        {
            MainThreadPoolExecutor.init(Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors(), 8, TimeUnit.SECONDS);
        }
        catch (IllegalOperationException e1)
        {
            logger.error("Error while initiating main thread pool", e1);
            e1.printStackTrace();
        }

        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    // Set System L&F
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                
                JFrame f = new MainWindow2();
                f.pack();
                f.setExtendedState(JFrame.MAXIMIZED_BOTH);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);
            }
        });
    }
}
